<?php

namespace Wap\Controller;

//收藏管理  1门店 2产品  3设计师
class CollectController extends WapController {

    protected $m;

    public function _initialize() {
        parent::_initialize();
        $this->m = D("Collect");
    }

    public function index() {
        //产品 、门店、设计师各展示一个
        $mem = $this->member;
        $where["mid"] = array("eq", $mem['id']);
        $where["type"] = array("eq", 2);
        $product = $this->m->relation("product")->where($where)->order("id desc")->limit(2)->select();



        $where["type"] = array("eq", 1);
        $store = $this->m->relation("store")->where($where)->order("id desc")->find();


        if($store['pic']!=null){
            $pic = explode(",",$store['pic']);
            $store['pic'] =$pic[0];//get_cover(intval($product['pic']),'path'); // 获取一张图片
        }

        $where["type"] = array("eq", 3);
        $designer = $this->m->relation("designer")->where($where)->order("id desc")->find();
        if($designer){
            $designer['pic'] = get_cover(intval($designer['pic']),'path');//_parse_pics($designer['pic'],true); // 获取一张图片
        }

        $where["type"] = array("eq", 4);
        $works = $this->m->relation("works")->where($where)->limit(2)->order("id desc")->select();

        $this->assign("product", $product);
        $this->assign("store", $store);
        $this->assign("designer", $designer);
        $this->assign("works", $works);
        if(IS_AJAX){
            $this->ajaxReturn(array('status'=>true,'html'=>$this->fetch('ajax_index')));
        }
        $this->display();
    }

    //得到收藏的产品
    public function get_product() {
        $mem = $this->member;
        $where["mid"] = array("eq", $mem['id']);
        $where["type"] = array("eq", 2);
        $list = $this->m->relation("product")->where($where)->select();
        foreach($list as &$item){
            $item['pic'] = get_cover(intval($item['pic']),'path');//_parse_pics($item['pic'],true); // 获取一张图片
        }
        $this->assign("list", $list);
        if(IS_AJAX){
            $this->ajaxReturn(array('status'=>true,'html'=>$this->fetch()));
        }
        $this->display();
    }

    //得到收藏的设计师
    public function get_designer() {
        $mem = $this->member;
        $where["mid"] = array("eq", $mem['id']);
        $where["type"] = array("eq", 3);
        $list = $this->m->relation("designer")->where($where)->select();
        foreach($list as &$item){
            //$item['pic'] = _parse_pics($item['pic'],true); // 获取一张图片
            $item['pic'] = get_cover(intval($item['pic']),'path'); // 获取一张图片
        }
        $this->assign("list", $list);
        if(IS_AJAX){
            $this->ajaxReturn(array('status'=>true,'html'=>$this->fetch()));
        }
        $this->display();
    }

    //得到收藏的门店
    public function get_store() {
        $mem = $this->member;
        $where["mid"] = array("eq", $mem['id']);
        $where["type"] = array("eq", 1);
        $list = $this->m->relation("store")->where($where)->select();
        foreach($list as &$item){
            if($item['pic']!=null){
                $pic = explode(",",$item['pic']);
                $item['pic'] =$pic[0];
                //$item['pic'] = _parse_pics($item['pic'],true); // 获取一张图片
            }

        }
        $this->assign("list", $list);
        if(IS_AJAX){
            $this->ajaxReturn(array('status'=>true,'html'=>$this->fetch()));
        }
        $this->display();
    }

    // 获取收藏的作品
    public function get_works(){
        $mem = $this->member;
        $where["mid"] = array("eq", $mem['id']);
        $where["type"] = array("eq", 4);
        $list = $this->m->relation("works")->where($where)->select();
        foreach($list as &$item){
            $pic = explode(",",$item['pic']);
            $item['pic'] = $pic[0]; // 获取一张图片
        }
        $this->assign("list", $list);
        if(IS_AJAX){
            $this->ajaxReturn(array('status'=>true,'html'=>$this->fetch()));
        }
        $this->display();
    }
}
